package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.pojo.dto.RoleQueryDto;
import com.community.pojo.entity.Role;
import com.community.pojo.entity.Role;
import com.community.pojo.entity.Result;
import com.community.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/role")
@Tag(name = "Role相关的接口")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")//代表访问路径
    @Operation(summary = "查询Role列表")
    @Tool(description = "查询Role列表")
    public Result list(@ToolParam @ModelAttribute RoleQueryDto roleQueryDto){

        //1.构建分页条件
        Page<Role> page=new Page<>(roleQueryDto.getPage(),roleQueryDto.getLimit());

        //2.构建查询条件
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(roleQueryDto.getName())){
            queryWrapper.like("name",roleQueryDto.getName());
        }
        if(!StringUtils.isEmpty(roleQueryDto.getPhone())){
            queryWrapper.eq("phone",roleQueryDto.getPhone());
        }

        //3.查询
        Page<Role> rolePage = roleService.page(page, queryWrapper);

        return Result.buildSuccess(rolePage.getRecords(),rolePage.getTotal());
    }


    @GetMapping("/find")
    @Operation(summary = "根据id查询Role信息")
    @Tool(description = "根据id查询Role信息")
    public Result find(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.查询数据
        Role role = roleService.getById(id);

        //2.返回结果
        return Result.buildSuccess(role);
    }


    @PostMapping("/add")
    @Operation(summary = "添加Role信息")
    @Tool(description = "添加Role信息")
    public Result add(@ToolParam @RequestBody Role role){//代表前端给后端发送Json数据

        role.setCreated(LocalDateTime.now());
        //1.调用service进行添加
        roleService.save(role);

        //2.返回结果
        return Result.buildSuccess();
    }


    @PostMapping("/update")
    @Operation(summary = "修改Role信息")
    @Tool(description = "修改Role信息")
    public Result update(@ToolParam @RequestBody Role role){//代表前端给后端发送Json数据
        //1.调用service进行修改
        roleService.updateById(role);

        //2.返回结果
        return Result.buildSuccess();
    }

    @GetMapping("/delete")
    @Operation(summary = "根据id删除Role信息")
    @Tool(description = "根据id删除Role信息")
    public Result delete(@ToolParam Long id){

        if(id==null){
            return Result.buildFail(20001,"id不能为空");
        }

        //1.调用service进行删除
        roleService.removeById(id);

        //2.返回结果
        return Result.buildSuccess();
    }

    @GetMapping("/findAll")
    @Operation(summary = "查询所有Role")
    @Tool(description = "查询所有Role")
    public Result findAll() throws Exception {

        //1.查询所有Role
        List<Role> roleList = roleService.list();

        //2.封装返回结果
        return Result.buildSuccess(roleList);
    }
}